<?php

class ClienteDao extends Dao {

    public function injectDependencies() {

    }

    public function findAll() {
        $StSql = TQuery::getSqlFindAllActiveCustomer();
        $this->initConnection();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while($ArCustomer = mysqli_fetch_assoc($tQuery)) {
            $ArCustomers[] = new Cliente($ArCustomer['IDCliente'], $ArCustomer, $this->tMysqli);
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    public function findWithBilletPayment() {
        $StSql = TQuery::getSqlFindAllActiveCustomerBoleto();
        $this->initConnection();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while($ArCustomer = mysqli_fetch_assoc($tQuery)) {
            $ArCustomers[] = new Cliente($ArCustomer['IDCliente'], $ArCustomer, $this->tMysqli);
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    //07-04
    public function save($ArDados) {
        $Usuario = Dracosys::getLoggedUser();
        $this->initTransaction();
        $this->tMysqli->escape($ArDados);

        $StSqlCreateCliente = TQuery::getSqlInsertCustomer($ArDados, $Usuario->getIdUsuario());
        $this->tMysqli->exec($StSqlCreateCliente);
        $IDCustomer = $this->tMysqli->lastInsertId();

        $StSqlCreateIndividual = TQuery::getSqlInsertIndividual($ArDados, $IDCustomer);
        $this->tMysqli->exec($StSqlCreateIndividual);
        $IDIndividual = $this->tMysqli->lastInsertId();

        if( !empty($ArDados['Enterprise']) ) {
            $StSqlCreateEnterprise = TQuery::getSqlInsertEnterprise($ArDados, $IDCustomer, $IDIndividual);
            $this->tMysqli->exec($StSqlCreateEnterprise);
        }

        $StSqlCreateFinancial = TQuery::getSqlInsertFinancial($IDCustomer, $ArDados);
        $this->tMysqli->exec($StSqlCreateFinancial);

        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function findById($IDCliente) {
        $this->initConnection();
        $this->tMysqli->escape($IDCliente);
        $StSql = TQuery::getSqlFindCustomerById($IDCliente);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while($ArCustomerData = mysqli_fetch_assoc($tQuery)) {
            $ArCustomers[] = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
        }
        $this->closeConnection();
        return !empty($ArCustomers) ? $ArCustomers[0] : false ;
    }

    public function findByDocument($ArDados) {
        $this->initConnection();
        $this->tMysqli->escape($ArDados);
        $StSql = TQuery::getSqlFindCustomerByDocument($ArDados);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while($ArCustomerData = mysqli_fetch_assoc($tQuery)) {
            $ArCustomers[] = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    //junho
    public function getListOfCustomerCities() {
        $this->initConnection();
        $StSql = TQuery::getSqlListarCidades();
        $this->tMysqli->exec($StSql);
        $ArCities = $this->tMysqli->fetchAll();
        $this->closeConnection();
        return $ArCities;
    }

    public function advancedSearch($ArDados) {
        $this->initConnection();
        $this->tMysqli->escape($ArDados);
        $StSql = TQuery::getSqlAdvancedSearchCustomer($ArDados);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while($ArCustomerData = mysqli_fetch_assoc($tQuery)) {
            $ArCustomers[] = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    public function countNumberOfCustomerRegisterByUser($IDUsuario, $ArPeriod) {
        $this->initConnection();
        $this->tMysqli->escape($IDUsuario);
        $StSql = TQuery::getSqlReportTotalCustomerByUser($IDUsuario, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $reportCustomer = $this->tMysqli->result(0, 'ReportCustomer');
        $this->closeConnection();
        return $reportCustomer;
    }

    public function numberOfCustomerRegisterByLoggedUser($ArPeriod = false) {
        return $this->countNumberOfCustomerRegisterByUser(Dracosys::getLoggedUser()->getIDUsuario(), $ArPeriod);
    }

    public function countNumberOfCustomerRegisterBySector($IDSetor, $ArPeriod = false) {
        $this->initConnection();
        $this->tMysqli->escape($IDSetor);
        $StSql = TQuery::getSqlReportTotalCustomerBySector($IDSetor, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $reportCustomer = $this->tMysqli->result(0, 'ReportCustomer');
        $this->closeConnection();
        return $reportCustomer;
    }

    public function countNumberOfCustomerRegisterByDepartament($IDDepartamento, $ArPeriod = false) {
        $this->initConnection();
        $this->tMysqli->escape($IDDepartamento);
        $StSql = TQuery::getSqlReportTotalCustomerByDepartament($IDDepartamento, $ArPeriod);
        $this->tMysqli->exec($StSql);
        $reportCustomer = $this->tMysqli->result(0, 'ReportCustomer');
        $this->closeConnection();
        return $reportCustomer;
    }

    public function update ($ArDados){
        $this->initTransaction();
        $this->tMysqli->escape($ArDados);
        $StSqlUpdateCustomer = TQuery::getSqlUpdateCustomer($ArDados['IDCliente'], $ArDados['General']);
        $this->tMysqli->exec($StSqlUpdateCustomer);
        $StSqlUpdateIndividual = TQuery::getSqlUpdateIndividual($ArDados['IDCliente'], $ArDados['Individual']);
        $this->tMysqli->exec($StSqlUpdateIndividual);
        if( !empty($ArDados['Enterprise']) ) {
            $StSqlUpdateEnterprise = TQuery::getSqlUpdateEnterprise ($ArDados['IDCliente'], $ArDados['Enterprise']);
            $this->tMysqli->exec($StSqlUpdateEnterprise);
        }
        $this->closeTransaction();
        return $this->tMysqli->lastTransactionReturn();
    }

    public function findByPayment($IDCobranca){
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlFindCustomerByPayment($IDCobranca);
        $tQuery = $this->tMysqli->exec($StSql);
        while ($ArCustomerData = mysqli_fetch_assoc($tQuery)){
            $Customer = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
        }
        $this->closeConnection();
        return isset($Customer) ? $Customer : false;
    }

    public function findByFinancial($IDFinanceiro) {
        $this->initConnection();
        $this->tMysqli->escape($IDFinanceiro);
        $StSql = TQuery::getSqlFindCustomerByFinancial($IDFinanceiro);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while($ArCustomerData = mysqli_fetch_assoc($tQuery)) {
            $ArCustomers[] = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
        }
        $this->closeConnection();
        return !empty($ArCustomers) ? $ArCustomers[0] : false ;
    }

    public function findCustomerWithPendingMonthly() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindCustomerWithPendingMonthly();
        $ArCustomers = array();
        $tQuery = $this->tMysqli->exec($StSql);
        while($ArCustomerData = mysqli_fetch_assoc($tQuery)) {
            $ArCustomers[] = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    public function findCustomersInId($ArCustomersId) {
        $this->initConnection();
        $this->tMysqli->escape($ArCustomersId);
        $StSql = TQuery::getSqlFindCustomersInIdList($ArCustomersId);
        echo $StSql;
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while($ArCustomerData = mysqli_fetch_assoc($tQuery)) {
            $ArCustomers[] = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    public function findOldersActiveCustomerInSystem($ItLimit) {
        $this->initConnection();
        $this->tMysqli->escape($ItLimit);
        $StSql = TQuery::getSqlGetOldersActiveCustomerInSystem($ItLimit);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while( $ArCustomerData = mysqli_fetch_assoc($tQuery) ) {
            $ArCustomers[] = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    public function findCustomerWithMoreCalledByType($EnTipoChamado, $ArStatus) {
        $this->initConnection();
        $this->tMysqli->escape($EnTipoChamado);
        $this->tMysqli->escape($ArStatus);
        $StSql = TQuery::getSqlFindCustomersWithMoreCalledByCalledType($EnTipoChamado, $ArStatus);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while($ArCustomerData = mysqli_fetch_assoc($tQuery)) {
             $Customer = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
             $Customer->setReport( array($ArCustomerData['NumberOfCalled']) );
             $ArCustomers[] = $Customer;
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    public function findCustomerWithMorePerfectCalleds() {
        $this->initConnection();
        $StSql = TQuery::getSqlFindCustomersWithMorePerfectCalled();
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while($ArCustomerData = mysqli_fetch_assoc($tQuery)) {
             $Customer = new Cliente($ArCustomerData['IDCliente'], $ArCustomerData, $this->tMysqli);
             $Customer->setReport( array($ArCustomerData['NumberOfPerfectCalled']) );
             $ArCustomers[] = $Customer;
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    public function findCustomerBySituation($EnSituacao) {
        $this->initConnection();
        $this->tMysqli->escape($EnSituacao);
        $StSql = TQuery::getSqlFindCustomerBySituation($EnSituacao);
        $tQuery = $this->tMysqli->exec($StSql);
        $ArCustomers = array();
        while($ArCustomerData = mysqli_fetch_assoc($tQuery)) {
            $ArCustomers[] = $this->findById($ArCustomerData['IDCliente']);
        }
        $this->closeConnection();
        return $ArCustomers;
    }

    public function findSimpleCustomerById($IDCliente) {
        return $this->findById($IDCliente);
    }

}

?>
